package view;

import dao.UserInfoDao;
import entity.UserInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class LoginFrame extends JFrame {
    public LoginFrame() throws HeadlessException {
        super();
        this.setTitle("登录");
        //this.setSize(400,300);
        this.setBounds(400,300,400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(flowLayout);
        JPanel jPanelUser = new JPanel();
        Dimension dimensionLabel = new Dimension(60,40);
        Dimension dimensionField = new Dimension(300,40);
        JLabel jLabelUser = new JLabel("用户名");
        jLabelUser.setPreferredSize(dimensionLabel);
        JTextField jTextFieldUser = new JTextField();
        jTextFieldUser.setPreferredSize(dimensionField);
        jPanelUser.add(jLabelUser);
        jPanelUser.add(jTextFieldUser);
        this.add(jPanelUser);


        JPanel jPanelpwd = new JPanel();

        JLabel jLabelpwd = new JLabel("密码");
        jLabelpwd.setPreferredSize(dimensionLabel);
        JPasswordField jpwdFieldpwd = new JPasswordField();
        //JTextField jTextFieldpwd = new JTextField();
        jpwdFieldpwd.setPreferredSize(dimensionField);
        jPanelpwd.add(jLabelpwd);
        jPanelpwd.add(jpwdFieldpwd);
        this.add(jPanelpwd);


        JButton jButton = new JButton("登录");
        jButton.setBackground(Color.MAGENTA);
        Font font = new Font("楷体",Font.BOLD,30);
        jButton.setFont(font);

        Dimension dimension = new Dimension(200,60);
        jButton.setPreferredSize(dimension);


        //按钮事件监听
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("----------我被点击-------------");

                String userName = new String(jTextFieldUser.getText());
                String userPwd = new String(jpwdFieldpwd.getPassword());
                UserInfoDao userInfoDao = new UserInfoDao();
                try {
                    UserInfo userInfo = userInfoDao.login(userName,userPwd);
                    if(userInfo != null){
                        //JOptionPane.showMessageDialog(null,"登录成功");
                        LoginFrame.this.setVisible(false);
                        //跳转到爬取/管理数据界面
                        new ReadOrManagerFrame().setVisible(true);

                    }else{
                        JOptionPane.showMessageDialog(null,"用户名或密码错误");
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }

            }
        });

        this.add(jButton);
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }


}
